import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:moose_app/ui/values/colors.dart';
import 'package:moose_app/ui/values/constants.dart';

class ATHOrderItem extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Container(
      constraints: BoxConstraints(minHeight: kSize300),
      margin: EdgeInsets.symmetric(horizontal: kSize20).copyWith(top: kSize10),
      child: Card(
        color: Colors.white,
        child: Container(
          margin: EdgeInsets.symmetric(horizontal: kSize16, vertical: kSize20),
          child: Column(
            children: [
              _buildTopMallInfo(),
              _buildOrderList(),
              _buildMoneyItem(),
              _buildOperatorItem()
            ],
          ),
        ),
      ),
    );
  }

  Widget _buildOrderList() {
    return ListView.builder(
        shrinkWrap: true,
        itemCount: 2,
        physics: NeverScrollableScrollPhysics(),
        itemBuilder: (context, index) {
          return _buildShopItem();
        });
  }

  Widget _buildShopItem() {
    return Container(
      margin: EdgeInsets.only(top: kSize20),
      child: Row(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Container(
            width: kSize180,
            height: kSize180,
            child: CachedNetworkImage(
              imageUrl:
                  "https://gitee.com/shizidada/moose-resource/raw/master/banner/banner-1.jpg",
              fit: BoxFit.fill,
            ),
          ),
          Expanded(
            child: Container(
              margin: EdgeInsets.symmetric(horizontal: kSize16),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Text(
                    "【活动价】大鸡腿好吃不要钱，快来下单抢购吧！！",
                    style: TextStyle(
                        fontSize: kSize28, fontWeight: FontWeight.bold),
                    maxLines: 2,
                    overflow: TextOverflow.ellipsis,
                  ),
                  Container(
                      margin: EdgeInsets.symmetric(vertical: kSize8),
                      child: Text(
                        "8个鸡腿：官方标配",
                        style:
                            TextStyle(color: kColor99, fontSize: kFontSize28),
                      )),
                  Text(
                    "七天无理由退货",
                    style: TextStyle(color: kColorFF6200),
                  ),
                ],
              ),
            ),
          ),
          Container(
            margin: EdgeInsets.symmetric(horizontal: kSize16),
            constraints: BoxConstraints(minWidth: kSize90, maxWidth: kSize120),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.end,
              children: [
                Text("¥ 998.00"),
                Text("x 1"),
              ],
            ),
          )
        ],
      ),
    );
  }

  Widget _buildOperatorItem() {
    return Container(
      margin: EdgeInsets.only(top: kSize30),
      child: Row(
        children: [
          Container(
              width: kSize100,
              child: Text(
                "更多",
                style: TextStyle(color: kColorF1, fontSize: kFontSize26),
              )),
          Expanded(
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                ATHOrderOperatorButton(
                  text: "加入购物车",
                ),
                ATHOrderOperatorButton(
                  text: "查看物流",
                ),
                ATHOrderOperatorButton(
                  text: "评价",
                  color: kColorFF6200,
                )
              ],
            ),
          )
        ],
      ),
    );
  }

  Widget _buildMoneyItem() {
    return Container(
      child: Column(
        children: [
          Container(
            margin: EdgeInsets.symmetric(vertical: kSize20),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                Expanded(
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: [
                      Text("运费险",
                          style: TextStyle(
                              color: kColor33,
                              fontSize: kFontSize28,
                              fontWeight: FontWeight.w500)),
                      Container(
                          margin: EdgeInsets.only(left: kSize16),
                          child: Text("退货可自动理赔",
                              style: TextStyle(
                                  color: kColor99, fontSize: kFontSize28))),
                    ],
                  ),
                ),
                Container(
                    width: kSize160,
                    alignment: Alignment.centerRight,
                    child: Text(
                      "已出单",
                      style: TextStyle(color: kColorFF6200),
                    ))
              ],
            ),
          ),
          Row(
            mainAxisAlignment: MainAxisAlignment.end,
            children: [
              ATHOrderPriceItem(
                text: "总价",
                price: "1000",
              ),
              ATHOrderPriceItem(
                text: "优惠",
                price: "2",
              ),
              ATHOrderPriceItem(
                text: "实付款",
                price: "998",
                color: kColor33,
              ),
            ],
          )
        ],
      ),
    );
  }

  Widget _buildTopMallInfo() {
    return Row(
      mainAxisAlignment: MainAxisAlignment.spaceBetween,
      crossAxisAlignment: CrossAxisAlignment.center,
      children: [
        Row(
          children: [
            SvgPicture.asset(
              "assets/icons/icon_trophy.svg",
              height: kSize40,
            ),
            Container(
              margin: EdgeInsets.symmetric(horizontal: kSize16),
              child: Text(
                "全栈技术部",
                style:
                    TextStyle(fontSize: kSize32, fontWeight: FontWeight.bold),
              ),
            ),
            SvgPicture.asset(
              "assets/icons/icon_arrow_right.svg",
              height: kSize40,
              color: kColorF1,
            )
          ],
        ),
        Text(
          "交易成功",
          style: TextStyle(fontSize: kFontSize28, color: kColorFF6200),
        ),
      ],
    );
  }
}

class ATHOrderOperatorButton extends StatelessWidget {
  final String text;

  final Color color;

  ATHOrderOperatorButton({this.text, this.color = kColor33});

  @override
  Widget build(BuildContext context) {
    return Container(
      alignment: Alignment.center,
      decoration: BoxDecoration(
          border: Border.all(color: color),
          borderRadius: BorderRadius.all(Radius.circular(kSize30))),
      constraints: BoxConstraints(minWidth: kSize180),
      height: kSize60,
      child: Text(
        text ?? "",
        style: TextStyle(fontSize: kFontSize26, color: color),
      ),
    );
  }
}

class ATHOrderPriceItem extends StatelessWidget {
  final String text;

  final String price;

  final Color color;

  ATHOrderPriceItem(
      {@required this.text, @required this.price, this.color = kColor99});

  @override
  Widget build(BuildContext context) {
    return Row(
      children: [
        Container(
          margin: EdgeInsets.only(left: kSize8),
          child: Text(
            text ?? "",
            style: TextStyle(fontSize: kFontSize30, color: color),
          ),
        ),
        Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          crossAxisAlignment: CrossAxisAlignment.end,
          children: [
            Text(
              " ¥ ",
              style: TextStyle(fontSize: kFontSize24, color: color),
            ),
            Text(
              price ?? "00",
              style: TextStyle(fontSize: kFontSize28, color: color),
            ),
            Text(
              ".00",
              style: TextStyle(fontSize: kFontSize24, color: color),
            ),
          ],
        )
      ],
    );
  }
}
